package service

import (
	"github.com/shopspring/decimal"
	"grpc_tapd_s/excel"
	"grpc_tapd_s/model"
)

func ExportXlsx(nowBegin, nowEnd, lastBegin, lastEnd, fileName string) {
	/*	//获取本周的开始结束时间
		nowBegin, nowEnd := tools.WeekIntervalTime(0)
		//获取上周的开始结束时间
		lastBegin, lastEnd := tools.WeekIntervalTime(-1)*/
	//获取本周bug
	bugs := model.GetAllBugs(nowBegin, nowEnd)
	//获取本周已完成
	completed := model.GetAlla(nowBegin, nowEnd, "resolved")
	//获取本周未完成
	processing := process(nowBegin, nowEnd, lastBegin, lastEnd)
	excel.ExportXlsx(bugs, completed, processing, fileName)
}
func isInSlice(need model.M, mm []model.M) (bool, string) {
	for _, m := range mm {
		//lastWeek, _ := strconv.Atoi(m.EffortCompleted)
		if need.Ids == m.Ids {
			return true, m.EffortCompleted
		}
	}
	return false, "0"
}

//获取处理中的需求
func process(begin, end, lastBegin, lastEnd string) []model.M {
	//获取上周的
	var mm = model.GetAll(lastBegin, lastEnd)
	//获取本周的
	var nowDatas = model.GetAll(begin, end)
	var exportData []model.M
	for _, data := range nowDatas {
		if data.Status == "rejected" || data.Status == "resolved" {
			continue
		}
		//状态都是未完成
		//本周的
		week := data.EffortCompleted
		//上周的
		isIn, effort := isInSlice(data, mm)

		//如果在上周里,并且这周的effort大于v上周的effort,也满足条件
		if isIn && week > effort {
			data.Last = effort
			x, _ := decimal.NewFromString(week)
			y, _ := decimal.NewFromString(effort)
			//y := decimal.NewFromFloat(0.99999)
			result := x.Sub(y)
			//result := x.Mul(y) //乘法
			//result := x.Add(y) //加法
			//fmt.Println(result)
			data.ThisEffort = result

			exportData = append(exportData, data)
		}
		//如果不在上周里,并且effort大于0,满足条件
		if !isIn && week > "0" {
			z, _ := decimal.NewFromString(week)
			data.ThisEffort = z
			exportData = append(exportData, data)
		}

	}
	return exportData
}
